import { ref, computed } from "vue";
import { defineStore } from "pinia";
import { useRouter } from "vue-router";
import { getRoleById, listRoleFunction } from "../services/role";

export const useRoleStore = defineStore("role", () => {
    const role = ref({});
    const roleId = ref();
    const toFunctionVisible = ref(false);
    const updateVisible = ref(false);
    const allFunctions = ref([]);
    const assignFunctions = ref([]);

    async function fetchRole(id) {
        let res = await getRoleById(id);
        role.value = res.data;
    }
    async function fetchRoleFunction(id) {
        roleId.value = id;
        let res = await listRoleFunction(id);
        let { all = [], assign = [] } = res.data;
        allFunctions.value = all.map((item) => ({
            key: item.id,
            label: item.name,
        }));
        assignFunctions.value = assign.map((item) => item.id);
    }

    return {
        roleId,
        role,
        updateVisible,
        toFunctionVisible,
        allFunctions,
        assignFunctions,
        fetchRole,
        fetchRoleFunction,
    };
});
